package com.hanhanxiaochong.zhongxiangchuanmei.view.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.CouponBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.CouponPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.ICouponView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.shop.adapter.CouponAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CouponActivity extends BaseActivity<CouponPresenter> implements ICouponView {


    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private static final int RESULT_COUPON = 0x0002;
    //project项目,good商品
    private String openFrom;
    private String p_id;
    private String token;
    private CouponAdapter couponAdapter;

    public static Intent createIntent(Context context, String openFrom, String p_id) {
        Intent intent = new Intent(context, CouponActivity.class);
        intent.putExtra("openFrom", openFrom);
        intent.putExtra("p_id", p_id);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_coupon);
        Intent intent = getIntent();
        openFrom = intent.getStringExtra("openFrom");
        p_id = intent.getStringExtra("p_id");
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new CouponPresenter(this);
    }

    @Override
    protected void init() {
        if (openFrom.equals("project")) {
            txtTitle.setText("项目优惠券");
            mPresenter.getProjectCoupons(token, p_id);
        } else {
            txtTitle.setText("产品优惠券");
            mPresenter.getGoodCoupons(token, p_id);
        }
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        List<CouponBean> list = new ArrayList<>();
        couponAdapter = new CouponAdapter(this, R.layout.item_coupon, list);
        recyclerview.setAdapter(couponAdapter);
        couponAdapter.setmOnItemClickListener((adapter, position) -> {
            CouponBean bean = couponAdapter.getDatas().get(position);
            Intent intent = new Intent();
            intent.putExtra("couponName", bean.getD_name());
            intent.putExtra("couponId", bean.getId());
            setResult(RESULT_COUPON, intent);
            finish();
        });
    }

    @OnClick(R.id.ll_leftview)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
        }
    }

    @Override
    public void setCouponList(List<CouponBean> data) {
        couponAdapter.setDatas(data);
    }
}
